package com.credithc.customers.dataservice.service.impl;

import com.credithc.baseserv.core.utils.UUIDUtils;
import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.base.mapper.CustomerProductMapper;
import com.credithc.customers.base.mapper.CustomerRelationInfoMapper;
import com.credithc.customers.base.po.CustomerProductInfoPo;
import com.credithc.customers.base.po.CustomerRelationInfoPo;
import com.credithc.customers.base.utils.CustomerUtils;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.mapper.JWBX.JWBXCustomerMapper;
import com.credithc.customers.dataservice.mapper.JWBX.JWBXOrderMapper;
import com.credithc.customers.dataservice.mapper.JWBX.JWBXProductMapper;
import com.credithc.customers.dataservice.mapper.JWBX.JWBXinsurdBackMapper;
import com.credithc.customers.dataservice.po.JWBXCusTomerPO;
import com.credithc.customers.dataservice.po.JWBXInsureBackPo;
import com.credithc.customers.dataservice.po.JWBXOrderPo;
import com.credithc.customers.dataservice.po.JWBXProductPO;
import com.credithc.customers.dataservice.ro.CustomerProductInfoRo;
import com.credithc.customers.dataservice.ro.ProductInfoRo;
import com.credithc.customers.dataservice.service.OldJWBXDataService;
import com.credithc.customers.dataservice.service.SyncDataService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;
@Service
public class OldJWBXDataServiceImpl implements OldJWBXDataService {

    @Autowired
    private JWBXProductMapper jwbxProductMapper;
    @Autowired
    private SyncDataService syncDataService;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;
    @Autowired
    private JWBXCustomerMapper jwbxCustomerMapper;
    @Autowired
    private JWBXinsurdBackMapper jwbXinsurdBackMapper;
    @Autowired
    private JWBXOrderMapper jwbxOrderMapper;
    @Autowired
    private CustomerProductMapper customerProductMapper;
    @Autowired
    private CustomerRelationInfoMapper customerRelationInfoMapper;
    @Override
    public void oldProduct(Integer start) {
        boolean f =true;
        Long num = null;
        if (start != null && start>0) {
            num = Long.valueOf(start);
        }else{
            num = jwbxProductMapper.queryOrderMaxId();
        }
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+10001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.execute(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-10000);
                    }
                    Example example = new Example(JWBXProductPO.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andBetween("id",vernier.get()-10000,vernier.get());
                    List<JWBXProductPO> jwbxProductPOS = jwbxProductMapper.selectByExample(example);
                    if (jwbxProductPOS.size() > 0) {
                        bulidJWBXProduct(jwbxProductPOS);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidJWBXProduct(List<JWBXProductPO> jwbxProductPOS) {
        for (JWBXProductPO p:jwbxProductPOS) {
            ProductInfoRo productInfoRo = new ProductInfoRo();
            productInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            productInfoRo.setDatabaseName("overseas_insurance");
            productInfoRo.setTableName("oi_products");
            productInfoRo.setProductChannel("JWBX");
            productInfoRo.setSubSystemSign("JWBX");
            productInfoRo.setSystemSign("BX");
            productInfoRo.setProductType(3+"");
            productInfoRo.setProductNo(p.getId()+"");
            productInfoRo.setTableKey(p.getId()+"");
            productInfoRo.setProductName(p.getName());
            try {
                productInfoRo.setProductCreateTime(p.getCreatTime());
            } catch (Exception e) {
            }
            syncDataService.syncProduct(productInfoRo);
        }
    }

    @Override
    public void oldOrder(Integer start) {
        boolean f =true;
        Long num = null;
        if (start != null && start>0) {
            num = Long.valueOf(start);
        }else{
            num = jwbxOrderMapper.queryOrderMaxId();
        }
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+10001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.execute(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-10000);
                    }
                    Example example = new Example(JWBXOrderPo.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andBetween("id",vernier.get()-10000,vernier.get());
                    List<JWBXOrderPo> jwbxOrderPos = jwbxOrderMapper.selectByExample(example);
                    if (jwbxOrderPos.size() > 0) {
                        bulidJWBXOrder(jwbxOrderPos);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidJWBXOrder(List<JWBXOrderPo> jwbxOrderPos) {
        for (JWBXOrderPo o :jwbxOrderPos) {
            CustomerProductInfoRo customerProductInfoRo = new CustomerProductInfoRo();
            customerProductInfoRo.setDatabaseName("overseas_insurance");
            customerProductInfoRo.setTableName("oi_proposalform");
            customerProductInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerProductInfoRo.setSubSystemSign("JWBX");
            customerProductInfoRo.setSystemSign("BX");
            customerProductInfoRo.setProductNo(o.getProductId());
            customerProductInfoRo.setProductSourceSystem("21");
            customerProductInfoRo.setProductStatus(CustomerProductInfoServiceImpl.getYZTStatus("JWBX"+o.getStatus()));
            customerProductInfoRo.setProductStartTime(CustomerUtils.dateStr2Timestamp(o.getSignTime()));
            // customerProductInfoRo.setProductEndTime(CustomerUtils.dateStr2Timestamp(map.get("policy_validity_date")));
            customerProductInfoRo.setProductDeadline(o.getRenewalPeriod()+"年");
            customerProductInfoRo.setTableKey(o.getId()+"");
            customerProductInfoRo.setProductAmount(o.getPremium());

            String tbrId = o.getCardId();
            String tbrName = o.getCn_name();
            if (StringUtils.isNotBlank(tbrId)) {
                //查询投保人信息
                Example tbE = new Example(JWBXCusTomerPO.class);
                Example.Criteria tbC = tbE.createCriteria();
                tbC.andEqualTo("cardNo", tbrId);
                //tbC.andEqualTo("name",tbrName);
                JWBXCusTomerPO jwbxCusTomerPO = null;
                try {
                    jwbxCusTomerPO = jwbxCustomerMapper.selectOneByExample(tbE);
                } catch (Exception e) {

                }
                if (jwbxCusTomerPO !=null && StringUtils.isNotBlank(jwbxCusTomerPO.getCustomerNo()) ) {

                    customerProductInfoRo.setUserResourceId("JWBX" + jwbxCusTomerPO.getCustomerNo());
                    customerProductInfoRo.setCustomerNo(jwbxCusTomerPO.getCustomerNo());
                    syncDataService.syncCustomerProduct(customerProductInfoRo);

                    if (customerProductInfoRo.getSyncType().equals(SyncTypeEnum.DELETE.getValue()) || customerProductInfoRo.getProductStatus() == 2 || customerProductInfoRo.getProductStatus() == 5 || customerProductInfoRo.getProductStatus() == 7) {
                        //根据订单信息修改角色可用性
                        Example example = new Example(CustomerProductInfoPo.class);
                        Example.Criteria criteria = example.createCriteria();
                        // criteria.andEqualTo("customerRole","3");
                        criteria.andEqualTo("subSystemSign", customerProductInfoRo.getSubSystemSign());
                        criteria.andEqualTo("customerNo", customerProductInfoRo.getCustomerNo());
                        List<Integer> list = new ArrayList(3);
                        list.add(2);
                        list.add(5);
                        list.add(7);
                        criteria.andNotIn("productStatus", list);
                        int i = customerProductMapper.selectCountByExample(example);
                        if (i < 1) {
                            customerProductInfoRo.setCustomerRoleStatus(CustomerConstants.CUSTOMER_ROLE_UNEFFECTIVE);
                        }
                    }
                    syncDataService.addRoleInfo(customerProductInfoRo.getUserResourceId(), customerProductInfoRo.getCustomerNo(), customerProductInfoRo.getSystemSign(), customerProductInfoRo.getSubSystemSign(), "3", customerProductInfoRo.getCustomerRoleStatus() == null ? CustomerConstants.STATUS_ACCOUNT_AVAILABLE:customerProductInfoRo.getCustomerRoleStatus());


                    Example example = new Example(JWBXInsureBackPo.class);
                    Example.Criteria criteria1 = example.createCriteria();
                    criteria1.andEqualTo("appointmentId",o.getAppointmentId());
                    JWBXInsureBackPo jwbxInsureBackPo = null;
                    try {
                        jwbxInsureBackPo = jwbXinsurdBackMapper.selectOneByExample(example);
                    } catch (Exception e) {

                    }

                    Example example1 = new Example(JWBXCusTomerPO.class);
                    Example.Criteria criteria = example1.createCriteria();
                    criteria.andEqualTo("cardNo", jwbxInsureBackPo.getCardId());
                    //criteria.andEqualTo("name", jwbxInsureBackPo.getCnName());
                   // criteria.andEqualTo("cardType", jwbxInsureBackPo.getCardType());
                    JWBXCusTomerPO jwbxCusTomerPO1 = null;
                    try {
                        jwbxCusTomerPO1 = jwbxCustomerMapper.selectOneByExample(example1);
                    } catch (Exception e) {

                    }
                    if (jwbxCusTomerPO1 != null && StringUtils.isNotBlank(jwbxCusTomerPO1.getCustomerNo())) {
                        syncDataService.addRoleInfo("JWBX" + jwbxCusTomerPO1.getCustomerNo(), jwbxCusTomerPO1.getCustomerNo(), customerProductInfoRo.getSystemSign(), customerProductInfoRo.getSubSystemSign(), "4", CustomerConstants.CUSTOMER_ROLE_EFFECTIVE);
                        //保存关系信息
                        CustomerRelationInfoPo customerRelationInfoPo = new CustomerRelationInfoPo();
                        customerRelationInfoPo.setSubSystemSign("JWBX");
                        customerRelationInfoPo.setSystemSign("BX");
                        customerRelationInfoPo.setRelationId("tbJWBX" + customerProductInfoRo.getCustomerNo() + "bbJWBX" + jwbxCusTomerPO1.getCustomerNo());
                        customerRelationInfoPo.setCustomerNo(customerProductInfoRo.getCustomerNo());
                        customerRelationInfoPo.setRoleInRelation(2);
                        CustomerRelationInfoPo customerRelationInfoPo1 = customerRelationInfoMapper.selectOne(customerRelationInfoPo);
                        if (customerRelationInfoPo1 == null) {
                            customerRelationInfoPo.setId(UUIDUtils.getUUID());
                            customerRelationInfoPo.setCreateTime(new Timestamp(System.currentTimeMillis()));
                            customerRelationInfoPo.setSubSystemSign("JWBX");
                            customerRelationInfoPo.setSystemSign("BX");
                            customerRelationInfoPo.setRelationId("tbJWBX" + customerProductInfoRo.getCustomerNo() + "bbJWBX" + jwbxCusTomerPO1.getCustomerNo());
                            customerRelationInfoPo.setRelationSource(2);
                            customerRelationInfoPo.setRoleInRelation(2);
                            customerRelationInfoMapper.insert(customerRelationInfoPo);
                        }
                        customerRelationInfoPo.setRoleInRelation(3);
                        customerRelationInfoPo.setCustomerNo(jwbxCusTomerPO1.getCustomerNo());
                        CustomerRelationInfoPo customerRelationInfoPo2 = null;
                        try {
                            customerRelationInfoPo2 = customerRelationInfoMapper.selectOne(customerRelationInfoPo);
                        } catch (Exception e) {
                            //
                        }
                        if (customerRelationInfoPo2 == null) {
                            customerRelationInfoPo.setId(UUIDUtils.getUUID());
                            customerRelationInfoPo.setCreateTime(new Timestamp(System.currentTimeMillis()));
                            customerRelationInfoPo.setRelationId("tbJWBX" + customerProductInfoRo.getCustomerNo() + "bbJWBX" + jwbxCusTomerPO1.getCustomerNo());
                            customerRelationInfoPo.setRelationSource(2);
                            customerRelationInfoPo.setRoleInRelation(3);
                            customerRelationInfoMapper.insert(customerRelationInfoPo);
                        }
                    }
                }
            }
        }
    }


}
